'use strict';

angular.module('app')
	.controller('loginController',[ '$rootScope', '$scope', '$http', '$state', function($rootScope, $scope, $http, $state) {
        $scope.user = {};
		$scope.time;
		$scope.image;
		$scope.login = function () {
            $.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/login',
				data: angular.toJson($scope.user),
                beforeSend :function(xmlHttp){
				    //登录时请求最新数据，不加载缓存数据
                    xmlHttp.setRequestHeader("If-Modified-Since","0");
                    xmlHttp.setRequestHeader("Cache-Control","no-cache");
                }
			}).then(function(result) {
				//console.log(result);
				if (result.httpCode == 200) {
					localStorage.setItem('projectId',result.projectId);//将项目id存到本地，新增人员及查询人员时要用到projeckId
					localStorage.setItem('userId',result.userId);//将用户id存到本地
                    GetMenuSecurity();//登陆成功加载个人权限回来，存到本地。
                    $state.go(result.menu);
                } else {
					$scope.msg = result.msg;
					 $scope.getVerifiCode();
					$rootScope.$apply();
				}
			});

        }
        //二维码初始隐藏
        $scope.dowloadClick = false;
		$scope.openDowload = function () {
            $scope.dowloadClick = true;
        }
        //点击关闭与返回关闭当前页面
        $scope.closeCode = function () {
            $scope.dowloadClick = false;
        }
        $scope.codeName = 'android';
        $scope.changeAndroid = function () {
            $scope.codeName = 'android';
            $("#qrCode").attr("src","res/img/androidAPPCode.png")
        }
        $scope.changeIos = function () {
            $scope.codeName = 'ios'
            $("#qrCode").attr("src","res/img/IosAPPCode.png")
        }
        function GetMenuSecurity() {
            $.ajax({
                type: 'PUT',
                contentType:'application/json;charset=UTF-8',
                url : '/menuSecurity/list'
            }).then(function(result) {
                $scope.loading = false;
                localStorage.setItem("PersonAuth",result)//个人权限存到本地，其他页面直接从本地取数据
                $scope.$apply();
            });
        }
        
        $scope.getVerifiCode = function() {
          	$scope.time = new Date().getTime();
          	$scope.user.time=$scope.time;
          	$('#image').attr("src","/verifiCode?time="+$scope.time);
          /*	$scope.time = new Date().getTime();
            $.ajax({
                type: 'PUT',
                url : '/verifiCode?time='+$scope.time
            }).then(function(result) {
                
                $scope.$apply();
            });
        */
        }
        $scope.getVerifiCode();
          


} ]);